<?php
namespace backend\controllers;

use Yii;
use yii\web\AdminController;
use backend\models\UploadForm;
use backend\models\GoodsIdentification;

class ArticleClassificationController extends AdminController
{
    public $layout = false;
    //业务关键字
    public $service = 'ArticleClassification';
    
    public $type = 2;
    
    public function actionIndex()
    {
        $model = new UploadForm();
        return $this->render('index',['model'=>$model]);
    }
    
    public function actionGetArticleClassification(){
        $data = Yii::$app->request->post();
        $query = GoodsIdentification::find()->select('id,name,sort,use_flg')->where('type='.$this->type)->orderBy('use_flg desc,edit_time desc');
        $count = $query->count();
        $list = $query->offset($data['start'])->limit($data['length'])->asArray()->all();
        $result['draw'] = intval($data['draw']);
        $result['recordsTotal'] = intval($count);
        $result['recordsFiltered'] = intval($count);
        $result['data'] = $list;
        return \yii\helpers\Json::encode($result);
    }
    
    public function actionChangeArticleClassificationStatus(){
        $model = new GoodsIdentification();
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $use_flg = $data['use_flg'];
        $data_new = [
            'use_flg'=>$use_flg,
            'editor'=>$model->getCreator(),
            'edit_time'=>date('Y-m-d H:i:s')
        ];
        $ret = $model::updateAll($data_new,['id'=>$id]);
        if(!$ret){
            throw new \Exception('操作失败');
        }
        $this->retJson('操作成功',200);
    }
    
    public function actionChangeArticleClassification(){
        $model = new GoodsIdentification();
        $data = Yii::$app->request->post();
        try {
            $goods_identification_id = $data['article_classification_id'];
            $name = $data['name'];
            if (!$name) {
                throw new \Exception('请填写商品标识');
            }
            $sort = $data['sort'];
            if (!$sort) {
                throw new \Exception('请填写排序编号');
            }
            $use_flg = $data['use_flg'];
            $oper = $model->getCreator();
            $operTime = date('Y-m-d H:i:s',time());
            if ($goods_identification_id){
                $model = $model->findOne($goods_identification_id);
                $model->editor = $oper;
                $model->edit_time = $operTime;
            }else{
                $model->creator = $oper;
                $model->create_time = $operTime;
                $model->type = $this->type;
            }
            $model->name = $name;
            $model->sort = $sort;
            $model->use_flg = $use_flg;
            $model->save();
            $id = $model->attributes['id'];
            if (!$id){
                throw new \Exception('文章信息操作失败');
            }
            $this->retJson('操作成功',200);
        } catch (\Exception $e) {
            //add by allen.qiang 20180112 异常处理，日志优化 start
            error_log('error->' . __METHOD__ . ' error:' . $e->getMessage());
            //add by allen.qiang 20180112 异常处理，日志优化 end
            throw new \Exception($e->getMessage(),500);
        }
    }
}